<?php

namespace Trawind\Workflows;

use Trawind\Workflows\Contracts\WorkflowInterface;
use Illuminate\Support\Facades\Route;
use Illuminate\Database\Eloquent\Model;

class Workflow extends Base implements WorkflowInterface
{
    use WorkflowQuery,
        WorkflowMainFlowMethods,
        WorkflowExecuteMethod;

    public $controllerIdentifier = '';
    public $actionIdentifier = '';
    public $eventType = '';
    public $triggerType;
    public $model;
    public $workflowCurrentExecuteState;
    public $translations;

    public function __construct(Model $model = null, $triggerType = null)
    {
        $this->domain = $this->makeDomain();
        $this->model = $model;
        $this->triggerType = $triggerType;
        $this->initialize();
    }

    private function initialize(): void
    {
        $this->initializeParams();
        $this->run();
    }

    private function initializeParams(): void
    {
        $routeAction = Route::currentRouteAction();
        $routeActionArr = explode('@', $routeAction);
        $this->controllerIdentifier = $routeActionArr[0];
        $this->actionIdentifier = $this->eventType = $routeActionArr[1];
    }

    protected function run()
    {
        $workflows = $this->workflow();
        // dump($workflows);
        if (!$workflows)
            return null;
        foreach ($workflows as $workflow) {
            $this->workflow = $workflow;
            $this->continueExecute();
        }
    }

    private function continueExecute()
    {
        $this->translationEnable = true;
        if (!$this->checkCurrentWorflowStateFlow())
            return null;
        $this->executeStateFlow();
        $this->translationFlow();
    }

    private function checkCurrentWorflowStateFlow(): ?bool
    {
        $res = $this->workflowCurrentExecuteState($this->workflow['id'], $this->model->id ?? null);
        $workflowCurrentExecuteState = $res['data']['data'][0] ?? null;

        if ($workflowCurrentExecuteState == null) {
            $this->startWorkflow($this->workflow, $this->model);
            $res = $this->workflowCurrentExecuteState($this->workflow['id'], $this->model->id ?? null);
            $workflowCurrentExecuteState = $res['data']['data'][0] ?? null;
        }
        $this->workflowCurrentExecuteState = $workflowCurrentExecuteState;
        if ($this->checkFinish($this->workflowCurrentExecuteState))
            return null;
        return true;
    }

    private function executeStateFlow()
    {
        if (!$this->stateCondition($this->workflowCurrentExecuteState))
            return $this->translationEnable = false;
        $conditions = $this->conditionsRequest($this->model->id ?? null, $this->workflow['id']);
        $this->executeState($this->workflowCurrentExecuteState, $conditions);
    }

    private function translationFlow()
    {
        if (!$this->translationEnable)
            return $this->workflowClientDisplay();
        $this->translations = $this->workflowTranslation($this->workflowCurrentExecuteState['current_state_id'])['data']['data'] ?? null;
        $this->workflowActionFinish();
        $this->translation();
        if ($this->buttonClickTranslation) {
            $this->buttonClickTranslation = false;
            return $this->continueExecute();
        }
        $this->workflowClientDisplay();
    }
}

// TODO action sort.
// TOOD multiple workflow cache workflow data.
